<template>
  <div class="edit-container">
    <el-form v-loading="formLoading" ref="form" :model="editData" :rules="rules" label-width="100px">
      <el-descriptions title="投诉信息：" :column="3" border>
        <el-descriptions-item labelStyle="font-weight: bold" label="房屋">{{ editData.house }}
        </el-descriptions-item>
        <el-descriptions-item labelStyle="font-weight: bold" label="简述">{{ editData.sketch }}
        </el-descriptions-item>
        <el-descriptions-item labelStyle="font-weight: bold" label="是否匿名">
          {{ editData.isAnonymous === 1 ? '是' : '否' }}
        </el-descriptions-item>
      </el-descriptions>

      <el-descriptions :column="1" border>
        <el-descriptions-item labelStyle="font-weight: bold" label="投诉内容">
          <!-- 图片展示区域 -->
          <div v-if="editData.images && editData.images.length > 0" class="image-container">
            <el-image
                v-for="(img, index) in editData.images"
                :key="index"
                :src="img.url"
                :preview-src-list="editData.images.map(i => i.url)"
                fit="cover"
                class="complaint-image"
            ></el-image>
          </div>
          <span v-else>无图片</span>
        </el-descriptions-item>
      </el-descriptions>

      <template>
        <div class="el-descriptions">
          <div style="height: 20px;"></div>
          <div class="el-descriptions__header">
            <div class="el-descriptions__title">回复内容：</div>
            <div class="el-descriptions__extra"></div>
          </div>
        </div>
      </template>

      <el-input type="textarea" :rows="4" v-model="form.replyContent"></el-input>

      <el-form-item>
        <el-button v-loading="saveLoading" element-loading-background="rgba(0, 0, 0, 0.3)" type="primary"
                   @click="handlerSubmit" v-has-permi="['admin:complaints:reply']">保存
        </el-button>
        <el-button @click="close">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import {reply} from "@/api/complaints";

export default {
  components: {},
  props: {
    editData: {
      type: Object,
      default: () => {
        return {
          house: '',
          sketch: '',
          isAnonymous: 0,
          images: [],  // 图片数组
          id: null,
          rules: []
        }
      }
    },
  },
  data() {
    return {
      form: {
        id: null,
        replyContent: '',
      },
      rules: {},
      roles: [],
      saveLoading: false,
      formLoading: false,
    }
  },
  mounted() {
    console.log(this.editData)
    // 模拟数据 - 实际使用时应该从props传入
    if (!this.editData.images || this.editData.images.length === 0) {
      this.editData.images = [
        { url: 'https://fuss10.elemecdn.com/a/3f/3302e58f9a181d2509f3dc0fa68b0jpeg.jpeg' },
        { url: 'https://fuss10.elemecdn.com/1/34/19aa98b1fcb2781c4fba33d850549jpeg.jpeg' }
      ];
    }
  },
  methods: {
    handlerSubmit() {
      if (this.saveLoading || this.formLoading) {
        return;
      }
      this.$message.success('保存成功');
      this.close();
      this.$refs.form.validate(valid => {
        if (!valid) return;
        this.saveLoading = true;
        this.form.id = this.editData.id
        reply(this.form).then(() => {
          this.$message.success('保存成功');
          this.close();
        }).finally(() => {
          this.saveLoading = false
        })
      })
    },
    close() {
      this.$emit('hideEditDialog')
    }
  }
}
</script>

<style scoped>
.edit-container {
  padding: 20px;
}

.image-container {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
}

.complaint-image {
  width: 150px;
  height: 150px;
  border-radius: 4px;
  cursor: pointer;
  transition: all 0.3s;
}

.complaint-image:hover {
  transform: scale(1.05);
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
}
</style>
